package com.example.demo.entity;


import io.swagger.annotations.ApiModel;
import lombok.Data;

import javax.persistence.*;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;


import java.sql.Date;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 
 * Description:用户信息
 * @author huangweicheng
 * @date 2019/10/21   
*/ 
@Entity
@Data
@ApiModel
@Table(name = "t_sys_user")
public class SysUserVo implements UserDetails
{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    private int id;

    @Column(name = "user_name")
    private String userName;

    @Column(name = "password")
    private String password;

    @Column(name = "b_use")
    private int bUse;

    @Column(name = "error_num")
    private int errorNum;

    @Column(name = "password_weak")
    private int passwordWeak;

    @Column(name = "forbid")
    private int forbid;

    @Column(name = "uuid")
    private String uuid;

    @Column(name = "create_time")
    private Date createTime;

    @Column(name = "last_update_time")
    private Date lastUpdateTime;


    /**
     * CascadeType.REMOVE 级联删除，FetchType.LAZY懒加载，不会马上从数据库中加载
     * name中间表名称
     * @JoinColumn t_sys_user的user_id与中间表user_id的映射关系
     * @inverseJoinColumns 中间表另一字段与对应表关联关系
    */
    @ManyToMany(cascade = CascadeType.REMOVE,fetch = FetchType.EAGER)
    @JoinTable(name = "t_sys_user_roles",joinColumns = @JoinColumn(name="user_id",referencedColumnName = "user_id"),inverseJoinColumns = @JoinColumn(name = "role_id",referencedColumnName = "role_id"))
    private List<SysRoleVo> roles;
    /** 
     * 
     * Description:权限信息
     * @param
     * @author huangweicheng
     * @date 2019/10/21   
    */ 
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities()
    {
        List<GrantedAuthority> authorityList = new ArrayList<>();
        List<SysRoleVo> roles = this.getRoles();
        for (SysRoleVo role : roles)
        {
            authorityList.add(new SimpleGrantedAuthority(role.getRoleName()));
        }
        return authorityList;
    }

    @Override
    public String getUsername()
    {
        return this.userName;
    }

    /**
     *
     * Description:账户是否过期
     * @param
     * @author huangweicheng
     * @date 2019/10/21
    */
    @Override
    public boolean isAccountNonExpired()
    {
        return true;
    }

    /**
     *
     * Description:账户是否被冻结
     * @param
     * @author huangweicheng
     * @date 2019/10/21
    */
    @Override
    public boolean isAccountNonLocked()
    {
        if (forbid != 1)
        {
            return false;
        }
        return true;
    }

    /**
     *
     * Description:账户密码是否过期，密码要求性高会使用到，比较每隔一段时间就要求用户重置密码
     * @param
     * @author huangweicheng
     * @date 2019/10/21
    */
    @Override
    public boolean isCredentialsNonExpired()
    {
        return true;
    }
    
    /** 
     * 
     * Description:账户是否可用
     * @param
     * @author huangweicheng
     * @date 2019/10/21   
    */ 
    @Override
    public boolean isEnabled()
    {

        if (bUse != 1)
        {
            return false;
        }
        return true;
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getbUse() {
        return bUse;
    }

    public void setbUse(int bUse) {
        this.bUse = bUse;
    }

    public int getErrorNum() {
        return errorNum;
    }

    public void setErrorNum(int errorNum) {
        this.errorNum = errorNum;
    }

    public int getPasswordWeak() {
        return passwordWeak;
    }

    public void setPasswordWeak(int passwordWeak) {
        this.passwordWeak = passwordWeak;
    }

    public int getForbid() {
        return forbid;
    }

    public void setForbid(int forbid) {
        this.forbid = forbid;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public List<SysRoleVo> getRoles() {
        return roles;
    }

    public void setRoles(List<SysRoleVo> roles) {
        this.roles = roles;
    }
    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getLastUpdateTime() {
        return lastUpdateTime;
    }

    public void setLastUpdateTime(Date lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }
}